import { Body, Controller, Delete, Get, Param, Post, Put, UsePipes } from '@nestjs/common';
import { PermissionService } from './permission.service';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import {
  CreatePermissionDto,
  UpdatePermissionDto,
  createPermissionSchema,
  updatePermissionSchema,
  paramSchema,
  rolePermAuthSchema,
  RolePermAuthDto
} from './dto/permission.schema';

@Controller('permissions')
export class PermissionController {
  constructor(private readonly permissionService: PermissionService) { }

  @Post()
  @UsePipes(new ZodValidationPipe(createPermissionSchema))
  create(@Body() createPermDto: CreatePermissionDto) {
    return this.permissionService.create(createPermDto);
  }

  @Get()
  findAll() {
    return this.permissionService.findAll();
  }

  @Get(':id')
  @UsePipes(new ZodValidationPipe(paramSchema))
  findOne(@Param() params: { id: string }) {
    return this.permissionService.findOne(+params.id);
  }

  @Put(':id')
  @UsePipes(new ZodValidationPipe(paramSchema))
  update(
    @Param() params: { id: string },
    @Body(new ZodValidationPipe(updatePermissionSchema)) updatePermDto: UpdatePermissionDto,
  ) {
    return this.permissionService.update(+params.id, updatePermDto);
  }

  @Delete(':id')
  @UsePipes(new ZodValidationPipe(paramSchema))
  remove(@Param() params: { id: string }) {
    return this.permissionService.remove(+params.id);
  }

  @Post('authorize-to-role')
  @UsePipes(new ZodValidationPipe(rolePermAuthSchema))
  authorizeToRole(@Body() authDto: RolePermAuthDto) {
    return this.permissionService.authorizeToRole(authDto);
  }

  @Get('role/:roleId')
  getRolePermissions(@Param('roleId') roleId: string) {
    return this.permissionService.getPermissionsByRoleId(+roleId);
  }
} 